package com.zjw.zy.coreui.dialog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zjw.zy.R;
import com.zjw.zy.base.BaseDialog;
import com.zjw.zy.base.BaseRecyclerAdapter;
import com.zjw.zy.coreui.fragment.adapter.ListDialogAdapter;
import com.zjw.zy.coreui.fragment.holder.ListTextHolder;
import com.zjw.zy.smi.JustWithIt;
import com.zjw.zy.smi.JustWithIt3;
import com.zjw.zy.utils.AppUtils;
import com.zjw.zy.utils.taskpool.MainExecutor;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/10/8 13:57
 */
public abstract class BaseListDialog<VALUE1, VALUE2, VALUE3> extends BaseDialog implements View.OnTouchListener {

    // 1, 2, 3
    private int listCount = 1;
    private RecyclerView mList;
    private RecyclerView mList2;
    private RecyclerView mList3;
    private Button mOk;
    private Button mCancel;
    private TextView mTitle;

    private List<String> mKeys;
    private List<String> mKeys2;
    private List<String> mKeys3;

    private List<VALUE1> mValues;
    private List<VALUE2> mValues2;
    private List<VALUE3> mValues3;

    private JustWithIt3<VALUE1, VALUE2, VALUE3> mPositiveListener;
    private JustWithIt3<VALUE1, VALUE2, VALUE3> mNegativeListener;
    private LinearLayoutManager layoutManager;
    private LinearLayoutManager layoutManager2;
    private LinearLayoutManager layoutManager3;

    private ListDialogAdapter mAdapter;
    private ListDialogAdapter mAdapter2;
    private ListDialogAdapter mAdapter3;

    BaseRecyclerAdapter.ItemListener<ListTextHolder> listener = new BaseRecyclerAdapter.ItemListener<ListTextHolder>() {
        @Override
        public void onItemClick(@NonNull ListTextHolder holder, @NonNull View v) {

        }

        @Override
        public boolean onItemLongClick(@NonNull ListTextHolder holder, @NonNull View v) {
            return false;
        }

        @Override
        public void onItemDoubleClick(@NonNull ListTextHolder holder, @NonNull View v) {

        }
    };

    private int mStartPosition = 0;
    private int mStartPosition2 = 0;
    private int mStartPosition3 = 0;
    private String mTitleTxt = "";


    protected void bindData(@NonNull List<String> keys, @NonNull List<VALUE1> values) {
        listCount = 1;
        this.mKeys = keys;
        this.mValues = values;

    }

    protected void bindData(@NonNull List<String> keys, @NonNull List<VALUE1> values
            , @NonNull List<String> keys2, @NonNull List<VALUE2> values2) {
        listCount = 2;
        this.mKeys = keys;
        this.mValues = values;

        this.mKeys2 = keys2;
        this.mValues2 = values2;
    }

    protected void bindData(@NonNull List<String> keys, @NonNull List<VALUE1> values
            , @NonNull List<String> keys2, @NonNull List<VALUE2> values2
            , @NonNull List<String> keys3, @NonNull List<VALUE3> values3) {
        listCount = 3;
        this.mKeys = keys;
        this.mValues = values;

        this.mKeys2 = keys2;
        this.mValues2 = values2;


        this.mKeys3 = keys3;
        this.mValues3 = values3;
    }

    protected BaseListDialog(@NonNull Context context) {
        super(context);
    }

    protected BaseListDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected BaseListDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onFindView() {
        mList = findViewById(R.id.list_dialog_list);
        mList2 = findViewById(R.id.list_dialog_list_2);
        mList3 = findViewById(R.id.list_dialog_list_3);
        mOk = findViewById(R.id.list_dialog_ok);
        mCancel = findViewById(R.id.list_dialog_cancel);
        mTitle = findViewById(R.id.list_dialog_title);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onBindListener() {
        mCancel.setOnClickListener(v -> {
            if (mNegativeListener != null) {
                mNegativeListener.onWith(getSelectValue(), getSelect2Value(), getSelect3Value());
            }
            dismiss();
        });
        mOk.setOnClickListener(v -> {
            //拿到中间的item
            if (mPositiveListener != null) {
                mPositiveListener.onWith(getSelectValue(), getSelect2Value(), getSelect3Value());
            }
            dismiss();
        });

        mList.setOnTouchListener(this);
        mList2.setOnTouchListener(this);
        mList3.setOnTouchListener(this);

    }

    private VALUE1 getSelectValue() {
        int itemPosition = layoutManager.findFirstVisibleItemPosition();
        return mValues.get(itemPosition);
    }

    private VALUE2 getSelect2Value() {
        if (listCount < 2) {
            return null;
        }
        int itemPosition = layoutManager2.findFirstVisibleItemPosition();
        return mValues2.get(itemPosition);
    }

    private VALUE3 getSelect3Value() {
        if (listCount < 3) {
            return null;
        }
        int itemPosition = layoutManager3.findFirstVisibleItemPosition();
        return mValues3.get(itemPosition);
    }


    @Override
    protected void onBindValue() {
        layoutManager = new LinearLayoutManager(AppUtils.getMainActivity());
        mAdapter = new ListDialogAdapter(listener);
        mList.setAdapter(mAdapter);
        mList.setLayoutManager(layoutManager);
        mList.scrollToPosition(mStartPosition);
        mAdapter.notifyData(mKeys);
        if (listCount >= 2) {
            layoutManager2 = new LinearLayoutManager(AppUtils.getMainActivity());
            mAdapter2 = new ListDialogAdapter(listener);
            mList2.setAdapter(mAdapter2);
            mList2.setLayoutManager(layoutManager2);
            mList2.scrollToPosition(mStartPosition2);
            mList2.setVisibility(View.VISIBLE);
            mAdapter2.notifyData(mKeys2);
        }

        if (listCount >= 3) {
            layoutManager3 = new LinearLayoutManager(AppUtils.getMainActivity());
            mAdapter3 = new ListDialogAdapter(listener);
            mList3.setAdapter(mAdapter3);
            mList3.setLayoutManager(layoutManager3);
            mList3.scrollToPosition(mStartPosition3);
            mList3.setVisibility(View.VISIBLE);
            mAdapter3.notifyData(mKeys3);
        }
        mTitle.setText(mTitleTxt);
    }

    @Override
    protected int onLayoutId() {
        return R.layout.dialog_list;
    }

    protected BaseListDialog<VALUE1, VALUE2, VALUE3> bindPositiveCallback(JustWithIt3<VALUE1, VALUE2, VALUE3> call) {
        mPositiveListener = call;
        return this;
    }


    protected BaseListDialog<VALUE1, VALUE2, VALUE3> bindNegativeCallback(JustWithIt3<VALUE1, VALUE2, VALUE3> call) {
        mNegativeListener = call;
        return this;
    }

    protected  BaseListDialog<VALUE1, VALUE2, VALUE3> select1Value(VALUE1 value) {
        int position = mValues.indexOf(value);
        if (position >= 0) {
            mStartPosition = position;
        }
        return this;
    }

    protected  BaseListDialog<VALUE1, VALUE2, VALUE3> select2Value(VALUE2 value) {
        int position = mValues2.indexOf(value);
        if (position >= 0) {
            mStartPosition2 = position;
        }
        return this;
    }

    protected  BaseListDialog<VALUE1, VALUE2, VALUE3> select3Value(VALUE3 value) {
        int position = mValues3.indexOf(value);
        if (position >= 0) {
            mStartPosition3 = position;
        }
        return this;
    }

    protected  BaseListDialog<VALUE1, VALUE2, VALUE3> title(@NotNull String title) {
        mTitleTxt = title;
        return this;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {
            MainExecutor.get().getMainHandler().postDelayed(() -> {
                int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
                mList.smoothScrollToPosition(firstVisibleItemPosition);
            }, 100L);
        }
        return false;
    }
}
